package com.hadwinling.alogriithm.projectforpso.ppppppp

import scala.collection.Seq

/**
 * @description: 设置粒子类
 * @author: hadwinling
 * @time: 2020/12/21 下午4:52
 */
class Particle(dimension: Int) extends Serializable {
  //粒子的位置
  var particle_position = (Seq.fill(dimension)(math.random)).toArray
  //粒子的速度
  var particle_velocity = (Seq.fill(dimension)(math.random)).toArray
  //局部最好位置
  var particle_best_position = (Seq.fill(dimension)(math.random)).toArray

  //当前粒子的适应值
  var particle_fitness = 0.0

  //局部最优解
  var particle_best_fitness = 0.0

  /**
   * 根据当前位置计算适应值
   *
   * @return
   */
  def fitness(x: Array[Double]): Double = {
    var temp: Double = 0
    for (dim <- 0 to x.length - 1) {
      temp = temp + (math.pow(x(dim), 2))
    }
    return temp
  }

  /**
   *计算当前适应值
   */
  def compute_particle_fitness() = {
    particle_fitness = fitness(particle_position)
  }

  /**
   * 计算最佳适应值
   */
  def compute_particle_best_fitness() = {
    particle_best_fitness = fitness(particle_best_position)
  }

}
